﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YJKRevitDimension.LevelDimension
{
	public partial class LevelStyleForm : System.Windows.Forms.Form
	{
		public string TextFont
		{
			get
			{
				return this.cmbTextStyle.Text;
			}
			set
			{
				this.cmbTextStyle.Text = value.ToString();
			}
		}

		public double TextHeight
		{
			get
			{
				return LevelDimensionManager.StrToDouble(this.hyTbTextHeight.Text, 0.0);
			}
			set
			{
				this.cmbTextStyle.Text = value.ToString("f1");
			}
		}

		public double AspectRatio
		{
			get
			{
				return LevelDimensionManager.StrToDouble(this.hyTbAspectRatio.Text, 0.0);
			}
			set
			{
				this.hyTbAspectRatio.Text = value.ToString("f1");
			}
		}

		public int Precision
		{
			get
			{
				return this.cmbPrecision.SelectedIndex;
			}
			set
			{
				this.cmbTextStyle.SelectedIndex = value;
			}
		}

		public int TextAlignment
		{
			get
			{
				return this.cmbAlignment.SelectedIndex;
			}
			set
			{
				this.cmbAlignment.SelectedIndex = value;
			}
		}

		public LevelStyleForm(ExternalCommandData cmdData, LevelDimensionForm dlgMain)
		{
			this.InitializeComponent();
			this.m_cmdData = cmdData;
			this.m_dlgMain = dlgMain;
		}

		private void LevelStyleForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.InitializeControl();
			this.InitializeTextStyle();
			this.ReadData();
			this.SetControlVisible();
		}

		private void InitializeControl()
		{
			this.cmbPrecision.Items.Add("0.00");
			this.cmbPrecision.Items.Add("0.000");
			this.cmbPrecision.SelectedIndex = 0;
			this.cmbAlignment.Items.Add("左侧");
			this.cmbAlignment.Items.Add("中心");
			this.cmbAlignment.Items.Add("右侧");
			this.cmbAlignment.SelectedIndex = 0;
		}

		private void SetComboBoxSelectedIndex(System.Windows.Forms.ComboBox control, int index)
		{
			if (index < 0 || index >= control.Items.Count)
			{
				return;
			}
			control.SelectedIndex = index;
		}

		private void ReadData()
		{
			try
			{
				this.m_strRevitVersion = this.m_cmdData.Application.Application.VersionNumber;
				string path = Product.UserDataLocation + "\\" + "LevelStyleForm.dat";
				if (!File.Exists(path))
				{
					this.SetFormInitialValue();
				}
				else
				{
					FileStream fileStream = new FileStream(path, FileMode.Open);
					StreamReader streamReader = new StreamReader(fileStream);
					streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
					bool flag = false;
					string text;
					while ((text = streamReader.ReadLine()) != null)
					{
						string[] array = text.Split(new char[]
						{
							'-'
						});
						int num = LevelDimensionManager.StrToInt(this.m_strRevitVersion, 0);
						int num2 = LevelDimensionManager.StrToInt(array[0], 0);
						if ((num > 2016 && num2 > 2016) || (num <= 2016 && num2 <= 2016))
						{
							this.cmbTextStyle.Text = array[1];
							this.hyTbTextHeight.Text = LevelDimensionManager.StrToDouble(array[2], 0.0).ToString("f1");
							this.hyTbAspectRatio.Text = LevelDimensionManager.StrToDouble(array[3], 0.0).ToString("f1");
							int index = Convert.ToInt32(array[4]);
							this.SetComboBoxSelectedIndex(this.cmbPrecision, index);
							index = Convert.ToInt32(array[5]);
							this.SetComboBoxSelectedIndex(this.cmbAlignment, index);
							flag = true;
							break;
						}
					}
					if (!flag)
					{
						this.SetFormInitialValue();
					}
					streamReader.Close();
					fileStream.Close();
				}
			}
			catch
			{
			}
		}

		private void SetFormInitialValue()
		{
			if (2016 < LevelDimensionManager.StrToInt(this.m_strRevitVersion, 0))
			{
				this.hyTbTextHeight.Text = "3.0";
			}
			else
			{
				this.hyTbTextHeight.Text = "3.5";
			}
			this.cmbTextStyle.Text = "仿宋";
			this.hyTbAspectRatio.Text = "0.7";
			this.cmbPrecision.Text = "0.000";
			this.cmbAlignment.Text = "右侧";
		}

		private void SaveData()
		{
			this.DeleteDataByVersion();
			FileStream fileStream = new FileStream(Product.UserDataLocation + "\\" + "LevelStyleForm.dat", FileMode.Append);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			string value = string.Concat(new string[]
			{
				this.m_strRevitVersion,
				"-",
				this.cmbTextStyle.Text,
				"-",
				this.hyTbTextHeight.Text,
				"-",
				this.hyTbAspectRatio.Text,
				"-",
				this.cmbPrecision.SelectedIndex.ToString(),
				"-",
				this.cmbAlignment.SelectedIndex.ToString()
			});
			streamWriter.WriteLine(value);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void DeleteDataByVersion()
		{
			try
			{
				string path = Product.UserDataLocation + "\\LevelStyleForm.dat";
				List<string> list = new List<string>(File.ReadAllLines(path));
				if (list.Count != 0)
				{
					FileStream fileStream = new FileStream(path, FileMode.Open);
					StreamReader streamReader = new StreamReader(fileStream);
					streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
					int num = 0;
					string text;
					while ((text = streamReader.ReadLine()) != null)
					{
						string[] array = text.Split(new char[]
						{
							'-'
						});
						if ((array[0] == "2016" && this.m_strRevitVersion == "2016") || (array[0] != "2016" && this.m_strRevitVersion != "2016"))
						{
							list.RemoveAt(num);
							break;
						}
						num++;
					}
					streamReader.Close();
					fileStream.Close();
					File.WriteAllLines(path, list.ToArray());
				}
			}
			catch
			{
			}
		}

		private void InitializeTextStyle()
		{
			this.cmbTextStyle.Items.Clear();
			FontFamily[] families = new InstalledFontCollection().Families;
			if (families == null || families.Length < 1)
			{
				return;
			}
			foreach (FontFamily fontFamily in families)
			{
				this.cmbTextStyle.Items.Add(fontFamily.Name);
			}
			this.cmbTextStyle.SelectedIndex = 0;
		}

		private void SetControlVisible()
		{
			if (this.m_dlgMain.Automatic && this.m_dlgMain.Associate)
			{
				this.cmbPrecision.Enabled = false;
				this.cmbAlignment.Enabled = false;
				return;
			}
			this.cmbPrecision.Enabled = true;
			this.cmbAlignment.Enabled = true;
		}

		public DimensionStyleParameter GetDimensionStyleParam()
		{
			return new DimensionStyleParameter
			{
				m_strTextFont = this.TextFont,
				m_dTextHeight = this.TextHeight,
				m_dAspectRatio = this.AspectRatio,
				m_nPrecision = this.Precision,
				m_nTextAlign = this.TextAlignment
			};
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.SaveData();
		}

		private void hyTbAspectRatio_Validating(object sender, CancelEventArgs e)
		{
			if ("" == this.hyTbAspectRatio.Text)
			{
				return;
			}
			this.hyTbAspectRatio.Text = Convert.ToDouble(this.hyTbAspectRatio.Text).ToString("f1");
			double num = Convert.ToDouble(this.hyTbAspectRatio.Text);
			if (1E-06 >= num)
			{
				YJKMessageBox.Information("高宽比的输入范围不能小于0");
				e.Cancel = true;
				return;
			}
		}

		private ExternalCommandData m_cmdData;

		private LevelDimensionForm m_dlgMain;

		private string m_strRevitVersion = "";
	}
}
